﻿using aier.dataaccess.model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Controllers;

namespace aier.wx.api.selfhost
{
    public class ApiTool
    {
        /// <summary>
        /// 获取当前登录用户对象
        /// </summary>
        /// <param name="RequestContext"></param>
        /// <returns></returns>
        public UserInfo GetLoginUser(HttpRequestContext RequestContext)
        {
            ClaimsIdentity identity = (ClaimsIdentity)RequestContext.Principal.Identity;
            Claim claim = identity.FindFirst("UserIdentity");
            UserInfo userInfo = JsonConvert.DeserializeObject<UserInfo>(claim.Value);
            return userInfo;
        }

        /// <summary>
        /// 随机数生成（用来生成外表主键）
        /// </summary>
        /// <param name="prefix">前缀</param>
        /// <param name="count">随机数长度</param>
        /// <returns></returns>
        public string GetRandomStr(string prefix, int count=16)
        {
            string[] s = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            Random random = new Random();
            StringBuilder sb = new StringBuilder();
            sb.Append(prefix);
            for (int i = 0; i < count; i++)
            {
                int index = random.Next(10);
                sb.Append(s[index]);
            }
            return sb.ToString();
        }


        /// <summary>
        /// 缩略图
        /// shidd
        /// </summary>
        /// <param name="myStream">文件流</param>
        /// <param name="suffix">图片扩展名 .jpg、.png、.bmp、.emf</param>
        /// <param name="width">缩略后图片宽</param>
        /// <param name="heigh">缩略后图片高</param>
        /// <returns>字节数组</returns>
        public byte[] toImage(Stream myStream, string suffix,int width=600,int heigh=400)
        {
            System.Drawing.Image thumbImage = null;
            try
            {
                //生成缩略图
                //创建改变后的Stream
                var stream = new MemoryStream();
                //创建一个图像对象取得上传图片对象
                System.Drawing.Image myImage = System.Drawing.Image.FromStream(myStream, false);
                //int percentage= myImage.Height / myImage.Width;//原图片比例
                //由上传图片产生一个缩略图(600*400)
                thumbImage = myImage.GetThumbnailImage(width, heigh, null, System.IntPtr.Zero);
                //保存缩略图到Stream流
                thumbImage.Save(stream, this.getImageFormat(suffix));
             
                return stream.ToArray();
            }
            finally
            {
                //关闭缩略图对象
                if (thumbImage != null)
                {
                    thumbImage.Dispose();
                }
            }
          
        }

        /// <summary>
        /// 图片格式
        /// </summary>
        /// <param name="suffix">文件后缀</param>
        /// <returns></returns>
        private System.Drawing.Imaging.ImageFormat getImageFormat(string suffix)
        {
            System.Drawing.Imaging.ImageFormat myFormat;
            switch (suffix.ToLower())
            {
                case ".bmp":
                    myFormat = System.Drawing.Imaging.ImageFormat.Bmp;
                    break;
                case ".emf":
                    myFormat = System.Drawing.Imaging.ImageFormat.Emf;
                    break;
                case ".exif":
                    myFormat = System.Drawing.Imaging.ImageFormat.Exif;
                    break;
                case ".gif":
                    myFormat = System.Drawing.Imaging.ImageFormat.Gif;
                    break;
                case ".icon":
                    myFormat = System.Drawing.Imaging.ImageFormat.Icon;
                    break;
                case ".jpeg":
                case ".jpg":
                    myFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
                    break;
                case ".png":
                    myFormat = System.Drawing.Imaging.ImageFormat.Png;
                    break;
                case ".tiff":
                    myFormat = System.Drawing.Imaging.ImageFormat.Tiff;
                    break;
                case ".wmf":
                    myFormat = System.Drawing.Imaging.ImageFormat.Wmf;
                    break;
                default:
                    myFormat = System.Drawing.Imaging.ImageFormat.MemoryBmp;
                    break;
            }
            return (myFormat);
        }

    }
}
